package org.budo.support.javassist.util;

import java.security.ProtectionDomain;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;

/**
 * @author limingwei
 */
public class JavassistUtil {
    public static void setSuperclass(CtClass ctClass, CtClass superClass) {
        try {
            ctClass.setSuperclass(superClass);
        } catch (CannotCompileException e) {
            throw new RuntimeException(e);
        }
    }

    public static CtClass get(ClassPool classPool, String classname) {
        try {
            return classPool.get(classname);
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static CtMethod make(String methodSrc, CtClass declaring) {
        try {
            return CtNewMethod.make(methodSrc, declaring);
        } catch (CannotCompileException e) {
            throw new RuntimeException(e);
        }
    }

    public static void addMethod(CtClass ctClass, CtMethod ctMethod) {
        try {
            ctClass.addMethod(ctMethod);
        } catch (CannotCompileException e) {
            throw new RuntimeException(e);
        }
    }

    public static Class toClass(CtClass ctClass, ClassLoader classLoader, ProtectionDomain protectionDomain) {
        try {
            return ctClass.toClass(classLoader, protectionDomain);
        } catch (CannotCompileException e) {
            throw new RuntimeException(e);
        }
    }
}